package com.linjm.cart.controller;

import com.alibaba.fastjson.JSON;
import com.linjm.bean.CartInfo;
import com.linjm.bean.SkuInfo;
import com.linjm.cart.service.CartCookieHandler;
import com.linjm.config.CookieUtil;
import com.linjm.config.LoginRequire;
import com.linjm.service.CartService;
import com.linjm.service.SpuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.CookieHandler;
import java.util.List;

@Controller

public class CarController {

    @Autowired
    private CartService cartService;
    @Autowired
    private CartCookieHandler cartCookieHandler;
    @DubboReference
    private SpuService spuService;

    @RequestMapping("addToCart")
    @LoginRequire(autoRedirect = false)
    public String addToCart(HttpServletRequest request,HttpServletResponse response){
        // 获取userId，skuId，skuNum
        String skuId = request.getParameter("skuId");
        String skuNum = request.getParameter("skuNum");
        String userId = (String) request.getAttribute("userId");
        // 判断用户是否登录
        if (userId!=null){
            // 说明用户登录,调用以下方法
            cartService.addToCart(skuId,userId,Integer.parseInt(skuNum));
        }else {
            // 说明用户没有登录,放到cookie中，调用以下方法
            cartCookieHandler.addToCart(request, response, skuId, null, Integer.parseInt(skuNum));
        }
        // 根据skuId 查询skuInfo
        SkuInfo skuInfo = spuService.getSkuInfo(skuId);
        request.setAttribute("skuNum",skuNum);
        request.setAttribute("skuInfo",skuInfo);
        return "success";
    }
    @RequestMapping("cartList")
    @LoginRequire(autoRedirect = false)
    public  String cartList(HttpServletRequest request,HttpServletResponse response){
        // 判断用户是否登录，登录了从redis中，redis中没有，从数据库中取,并把数据存到redis
        // 没有登录，从cookie中取得
        String userId = (String) request.getAttribute("userId");

        List<CartInfo> cartInfoList = null;
        if(userId!=null){
            //合并购物车
            List<CartInfo> cartListCK = cartCookieHandler.getCartList(request);
            if (cartListCK!=null){
                //合并购物车
                cartInfoList = cartService.mergeToCartList(cartListCK,userId);
                //删除购物车
                cartCookieHandler.deleteCartCookie(request,response);
            }else {
                //从redis中取得，或者从数据库中取
                cartInfoList = cartService.getCartList(userId);
            }
        }else {
            //调用未登陆状态下的方法添加购物车
            cartInfoList=cartCookieHandler.getCartList(request);
        }
        request.setAttribute("cartInfoList", cartInfoList);
        return "cartList";
    }

    @RequestMapping("delCookie")
    @ResponseBody
    public  String delCookie(HttpServletRequest request,HttpServletResponse response){
        //删除购物车
        cartCookieHandler.deleteCartCookie(request,response);
        return "删除cookie成功";
    }
    @RequestMapping("checkCart")
    @ResponseBody
    @LoginRequire(autoRedirect = false)
    public void checkCart(HttpServletRequest request,HttpServletResponse response){
        String isChecked = request.getParameter("isChecked");
        String skuId = request.getParameter("skuId");

        String userId = (String) request.getAttribute("userId");
        //登陆状态
        if (userId!=null){
            cartService.checkCart(skuId,isChecked,userId);
        }else {
            //未登陆状态
            cartCookieHandler.checkCart(request,response,skuId,isChecked);
        }
    }

    @RequestMapping("toTrade")
    @LoginRequire
    public String toTrade(HttpServletRequest request,HttpServletResponse response){
        // 合并勾选的商品 未登录+登录
        List<CartInfo> cartListCK = cartCookieHandler.getCartList(request);
        String userId = (String) request.getAttribute("userId");
        if (cartListCK!=null){
            //合并
            cartService.mergeToCartList(cartListCK,userId);
            //删除为登陆数据
            cartCookieHandler.deleteCartCookie(request,response);
        }

        return "redirect://order.gmall.com/trade";
    }

}
